/**
 * @param {number[]} nums
 * @return {number}
 */
var maxProduct = function (nums) {
    const n = nums.length;
    if (n == 1) return nums[0];
    // maxArr[i]最大连续正数，minArr最大连续负数
    const maxArr = new Array(n).fill(0);
    const minArr = new Array(n).fill(0);
    if (nums[0] > 0) maxArr[0] = nums[0];
    else minArr[0] = nums[0];
    for (let i = 1; i < nums.length; i++) {
        if (nums[i] > 0) {
            maxArr[i] = Math.max(maxArr[i - 1] * nums[i], nums[i]);
            minArr[i] = minArr[i - 1] * nums[i];
        } else if (nums[i] == 0) {
            maxArr[i] = 0;
            minArr[i] = 0;
        } else if (nums[i] < 0) {
            maxArr[i] = Math.max(minArr[i - 1] * nums[i], nums[i]);
            minArr[i] = Math.min(maxArr[i - 1] * nums[i], nums[i]);
        }
    }
    console.log(maxArr);
    console.log(minArr);
    return Math.max(...maxArr);
};
var maxProduct = function (nums) {
    const n = nums.length;
    if (n == 1) return nums[0];
    // maxArr[i]最大连续正数，minArr最大连续负数
    let maxArr = 0;
    let minArr = 0;
    if (nums[0] > 0) maxArr = nums[0];
    else minArr = nums[0];
    let tmpmax, tmpmin;
    let res = maxArr;
    for (let i = 1; i < nums.length; i++) {
        tmpmax = maxArr;
        tmpmin = minArr;
        if (nums[i] > 0) {
            tmpmax = Math.max(maxArr * nums[i], nums[i]);
            tmpmin = Math.min(minArr * nums[i], nums[i]);
        } else if (nums[i] == 0) {
            tmpmax = 0;
            tmpmin = 0;
        } else {
            tmpmax = Math.max(minArr * nums[i], nums[i]);
            tmpmin = Math.min(maxArr * nums[i], nums[i]);
        }
        maxArr = tmpmax;
        minArr = tmpmin;
        res = Math.max(res, maxArr);
    }
    return res;
};

// console.log(maxProduct([2, 3, -2, 4]));
// console.log(maxProduct([-2, 0, -1]));
// console.log(maxProduct([-2, 1, 2]));
// console.log(maxProduct([2, -2, 3, -3]));
// console.log(maxProduct([-2]));
// console.log(maxProduct([2, -5, -2, -4, 3]));
// console.log(maxProduct([2, -1, 1, 1]));
